import React, {useState} from "react"
import GoodsTable, {GoodsTableDataItem, GoodsTableProps} from "../components/GoodsTable";
import {castFilterValue, mergePaginationAndPageable} from "../utils/AntdTableUtils";
import {Pageable} from "../utils/Page";
import {TablePaginationConfig} from "antd";
import {type FilterValue, SorterResult, type TableCurrentDataSource} from "antd/es/table/interface";

export interface OnlinePaginationGoodsTableProps<T> extends GoodsTableProps<T> {
    onConditionChange: (condition: QueryGoodsTableDataCondition<T>,pagination: TablePaginationConfig, filters: Record<string, FilterValue | null>, sorter: SorterResult<T> | SorterResult<T>[], extra: TableCurrentDataSource<T>) => void
}

export interface QueryGoodsTableDataCondition<T> {
    name?: string
    barcode?: string
    brandIds?: number[]
    categoryIds?: number[]
    factoryIds?: number[]
    pagination?: TablePaginationConfig
    sorter?:SorterResult<T>|SorterResult<T>[]
}


function OnlinePaginationGoodsTable<T extends GoodsTableDataItem>(props: OnlinePaginationGoodsTableProps<T>) {
    return (
        <>
            <GoodsTable<T> {...props} onChange={(pagination, filters, sorter,extra) => {
                props.onConditionChange(
                    {
                        name: castFilterValue(filters.name),
                        barcode: castFilterValue(filters.barcode),
                        brandIds: filters.brand as number[],
                        categoryIds: filters.category as number[],
                        factoryIds: filters.factory as number[],
                        pagination: pagination,
                        sorter:sorter,
                    },
                    pagination,
                    filters,
                    sorter,
                    extra,
                )
            }} key={"id"}/>
        </>
    )
}

export default OnlinePaginationGoodsTable
